<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      var btn = document.createElement("button");
      btn.innerText = "点击切换背景";
      document.body.appendChild(btn);
      // var imgarr = [
      //   "images/1.png",
      //   "images/2.png",
      //   "images/3.png",
      //   "images/4.png",
      //   "images/5.png",
      //   "images/6.png",
      //   "images/7.png",
      //   "images/8.png",
      //   "images/9.png",
      //   "images/10.png",
      // ]; //第二种 运用在图片名字不规律的时候
      var previousIndex = 0;
      function randomNumber(min, max) {
        var res = parseInt(Math.random() * (max - min)) + min;
        while (previousIndex == res) {
          res = parseInt(Math.random() * (max - min)) + min;
        }
        previousIndex = res;
        return res;
      } //返回和上次不重复的随机数
      btn.onclick = function () {
        var num = randomNumber(1, 10);
        document.body.style.background = `url(../day 13/images/${num}.png) no-repeat top left/100vw 100vh`; //第一种 图片名规律的时候
        //   document.body.style.background = `url(${imgarr[num-1]}) no-repeat top left/100vw 100vh`; ////第二种 随机数给数组
      };
    </script>
  </body>
</html>
